package com.example.demo1.service;

import com.example.demo1.Entity.Student;
import com.example.demo1.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentRepository studentRepository;  // 修正拼写错误

    @Transactional
    @Override
    public Student StudentSet(long studentid,
                              String studentname,
                              String studentsex,
                              String clas,
                              String studentinformation,
                              long courseid,
                              String coursename
                              ) {
        Student student = new Student();
        student.setStudentid(studentid);
        student.setStudentname(studentname);
        student.setStudentsex(studentsex);
        student.setClas(clas);
        student.setStudentinformation(studentinformation);
        student.setCourseid(courseid);
        student.setCoursename(coursename);
        return student;
    }

    @Transactional
    @Override  // 添加了@Override注解
    public void save_data(Student E) {
        studentRepository.save(E);  // 使用注入的repository实例
        System.out.println("插入成功！ID: " + E.getId());
    }

    @Override  // 添加了@Override注解
    public Student getStudentByStudentid(long studentid) {
        return studentRepository.findByStudentid(studentid).orElse(null);  // 修正方法名拼写和使用注入的repository
    }
}